package healththink

import grails.test.mixin.*
import org.junit.*

/**
 * See the API for {@link grails.test.mixin.domain.DomainClassUnitTestMixin} for usage instructions
 */
@TestFor(Facility)
@Mock([Facility, FacilityAttributeLink, Attribute])
class FacilityTests {

	private Facility getFacility()
	{
		Facility fac = HealthThink.getStaticFacility()
		fac.facilitySite = HealthThink.getStaticFacilitySite()
		
		return fac
	}

	@Test
    void testFacility()
	{
		Facility fac = getFacility()
		   
		if(fac.validate())
		{
			assertNotNull("Saving Facility", fac.save(flush:true))
			fac.delete(flush:true)
		}
		else
		{
			fail("Facility Not Validated")
		}
	   
		Attribute fa = HealthThink.getStaticFacilityAttribute()
		FacilityAttributeLink fal = HealthThink.getStaticFacilityAttributeLink()

		fa.addToFacilities(fal)
		fac.addToFacilityAttributes(fal)
		fac.save(flush:true)
	   
		Facility expected = Facility.findById(fac.id)
		   
		assertNotNull("(error)FacilityAttributeLink Target was not saved", fac)
		assertNotNull("(error)Expected FacilityAttributeLink was null", expected)
		assertEquals("(error)Target and Expected are not Equal", fac, expected)
		
		String facList = fac.facilityAttributes.toString()
		String expectedList = expected.facilityAttributes.toString()
		assertEquals("(error)Target ATTR and Expected ATTR are not Equal", 
			fac.facilityAttributes, 
			expected.facilityAttributes)
		   
		fac.delete(flush:true)
    }
}
